// pages/warehouse-home/warehouse-home.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    params: { page: 1 },
    params1: { page: 1 },
    params2: { page: 1 },
    params3: { page: 1 },
    ware: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.data) {
      let { type } = JSON.parse(options.data)
      this.setData({
        active: 2
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getWareHouse()
    this.getTorehoucollarTorehouseClerk()
    this.getauxiliarymateri()
    this.getTorehouseClerk()
  },
  loadMore(e) {
    let { params, ware } = this.data
    this.setData({
      'params.page': params.page + 1
    }, () => {
      app.reqFetch.wareHouseHome({
        ...this.data.params
      }, (res) => {
        const { list = [] } = res.list
        this.setData({
          ware: ware.concat(list)
        })
      })
    })
  },
  loadMore1(e) {
    let { params1, ware1 } = this.data
    this.setData({
      'params1.page1': params1.page + 1
    }, () => {
      app.reqFetch.collarTorehouseClerk({
        ...this.data.params1
      }, (res) => {
        const { list = [] } = res.list
        this.setData({
          ware1: ware1.concat(list)
        })
      })
    })
  },
  loadMore2(e) {
    let { params2, ware2 } = this.data
    this.setData({
      'params2.page': params2.page + 1
    }, () => {
      app.reqFetch.auxiliarymateri({
        ...this.data.params2
      }, (res) => {
        const { list = [] } = res.list
        this.setData({
          ware2: ware2.concat(list)
        })
      })
    })
  },
  loadMore3(e) {
    let { params3, ware3} = this.data
    this.setData({
      'params3.page': params3.page + 1
    }, () => {
      app.reqFetch.pagesTorehouseClerk({
        ...this.data.params3
      }, (res) => {
        const { list = [] } = res.list
        this.setData({
          ware3: ware3.concat(list)
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  onChange(event) {
    if (event.detail === 1) {
      wx.redirectTo({
        url: '/pages/mine/mine',
      })
    }
  },

  toDetails(e) {

    const { stockOrderId, status, auxiliaryMaterialsId, smallCanteenPurchaseId, collarId, flag } = e.currentTarget.dataset.record
    const type = e.currentTarget.dataset.type
    const oreder={
      stockOrderId,
      auxiliaryMaterialsId,
      smallCanteenPurchaseId,
      collarId
    }
    if (flag === 1) {
      app.globalData.applyType = 1
      wx.navigateTo({
        url: '/pages/purchase-list/purchase-list?data=' + JSON.stringify({}),
      })
      return
    }
    if (type === 0||type===2||type===3) {
      if (status === 3) {
        wx.navigateTo({
          url: '/pages/warehouse-details/warehouse-details?data=' + JSON.stringify({ ...oreder,  audit: true, type }),
        })
      } else {
        wx.navigateTo({
          url: '/pages/warehouse-details/warehouse-details?data=' + JSON.stringify({ ...oreder, readonly: true, type }),
        })
      }
    } else {
      app.globalData.revise = false
      if (status === 1) {
        this.getDetails(auxiliaryMaterialsId)
        app.globalData.applyType = 1
        app.globalData.auxiliaryMaterialsId = auxiliaryMaterialsId
        app.globalData.revise = true
      } else if (status === 3) {
        wx.navigateTo({
          url: '/pages/warehouse-details/warehouse-details?data=' + JSON.stringify({ auxiliaryMaterialsId, audit: true, type }),
        })
      } else if (status === 4){
        wx.navigateTo({
          url: '/pages/warehouse-details/warehouse-details?data=' + JSON.stringify({ auxiliaryMaterialsId, readonly: true, type }),
        })
      }else{
        wx.navigateTo({
          url: '/pages/admin-purchase-list/admin-purchase-list?data=' + JSON.stringify({ auxiliaryMaterialsId, chenck: true, type }),
        })
      }
    }
  },
  getWareHouse() {
    app.reqFetch.wareHouseHome({
    }, (res) => {
      this.setData({
        ware: res.list.list,
        pending: res.pending,
      })
    })
  },

  getTorehoucollarTorehouseClerk() {
    app.reqFetch.collarTorehouseClerk({
    }, (res) => {
      this.setData({
        ware1: res.list.list,
        pending1: res.pending,
      })
    })
  },
  getauxiliarymateri() {
    app.reqFetch.auxiliarymateri({
    }, (res) => {
      this.setData({
        ware2: res.list.list,
        pending2: res.pending,

      })
    })
  },

  getTorehouseClerk() {
    app.reqFetch.pagesTorehouseClerk({
    }, (res) => {
      this.setData({
        ware3: res.list.list,
        pending3: res.pending,

      })
    })
  },
  application() {
    app.globalData.applyType = 1
    app.globalData.orderList = []
    
    wx.navigateTo({
      url: '/pages/supply-menu/supply-menu',
    })
  },
  
  getDetails(auxiliaryMaterialsId) {
    app.reqFetch.auxiliarymaterialsmxDetails({ auxiliaryMaterialsId }, (res) => {
      const {  list } = res
      const {type} =this.data
      let orderList = []
      list.map(item => {
        orderList.push({ supplierId: item.gysId, supplierName: item.gys, foods: item.mxlist })
      })
      app.globalData.orderList = orderList
      wx.navigateTo({
        url: '/pages/purchase-list/purchase-list?data=' + JSON.stringify({ auxiliaryMaterialsId, type, revise: true })
      })

    })

  }


})